package com.runqian.report.view.excel;

import java.util.regex.Pattern;

import com.runqian.base.util.Logger;
import com.runqian.base.util.Native2Ascii;
import com.runqian.report.cellset.CellSet;
import com.runqian.report.view.LicenseException;
import com.runqian.report.view.ReportCalculator;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ExcelReportServlet extends HttpServlet {
	public void service(HttpServletRequest paramHttpServletRequest,
			HttpServletResponse paramHttpServletResponse)
			throws ServletException {
		ServletOutputStream localServletOutputStream = null;
		int ratio = 100;
		try {
			paramHttpServletRequest.setCharacterEncoding("GBK");
			ReportCalculator localReportCalculator = new ReportCalculator(
					super.getServletContext());
			CellSet localCellSet = null;
			String str1;
			if ((str1 = paramHttpServletRequest.getParameter("cachedId")) != null) {
				localReportCalculator.setParameter("cachedId", str1);
				localCellSet = localReportCalculator.calc();
			}

			String str2;
			if ((str2 = paramHttpServletRequest.getParameter("file")) == null) {
				throw new Exception("请输入文件名参数!");
			}
			str2 = Native2Ascii.decode(str2);
			String str3;
			if (localCellSet == null) {
				(localReportCalculator = new ReportCalculator(
						super.getServletContext())).setParameter("fileName",
						str2);
				str3 = paramHttpServletRequest.getParameter("srcType");
				localReportCalculator.setParameter("srcType", str3);
				String str4;
				if ((str4 = paramHttpServletRequest
						.getParameter("reportParamsId")) != null) {
					localReportCalculator.setParameter("reportParamsId", str4);
				}
				String str5;
				if ((str5 = paramHttpServletRequest.getParameter("connection")) != null) {
					localReportCalculator.setParameter("connection", str5);
				}
				try {
					localCellSet = localReportCalculator.calc();
				} catch (LicenseException localLicenseException) {
					throw new Exception(localLicenseException.getMessage());
				}
			}

			if ((str3 = paramHttpServletRequest.getParameter("saveAsName")) == null) {
				int i;
				if ((i = str2.lastIndexOf(".")) > 0) {
					str2 = str2.substring(0, i);
				}

				if ((i = str2.lastIndexOf("/")) >= 0) {
					str2 = str2.substring(i + 1);
				}
				str3 = str2;
			} else {
				str3 = Native2Ascii.decode(str3);
			}
			str2 = new String(str3.getBytes("GBK"), "iso-8859-1");
			paramHttpServletResponse.setContentType("application/x-msdownload");
			paramHttpServletResponse.setHeader("Content-Disposition",
					"attachment; filename=" + str2 + ".xls");
			Object tmp = paramHttpServletRequest.getSession().getAttribute("RATIO_"+str3);
			if(tmp!=null&&Pattern.matches("\\d+", tmp.toString())){
				ratio = Integer.parseInt(tmp.toString());
			}
			int i = 0;
			int j = 0;
			int k = 0;
			String str6;
			if ((str6 = paramHttpServletRequest.getParameter("pageStyle")) == null)
				str6 = "0";
			if (str6.equals("0")) {
				i = -1;
				j = -1;
			} else {
				try {
					i = Integer.parseInt(paramHttpServletRequest
							.getParameter("width"));
				} catch (Exception localException1) {
				}
				try {
					j = Integer.parseInt(paramHttpServletRequest
							.getParameter("height"));
				} catch (Exception localException2) {
				}
			}
			try {
				k = Integer.parseInt(paramHttpServletRequest
						.getParameter("columns"));
			} catch (Exception localException3) {
			}
			ExcelReport localExcelReport = new ExcelReport(localCellSet, i, j,
					k);
			try {
				localServletOutputStream = paramHttpServletResponse
						.getOutputStream();
				localExcelReport.setDispRatio(ratio);
				localExcelReport.out(localServletOutputStream);
				localServletOutputStream.close();

				return;
			} catch (Exception localException5) {
				return;
			}

		} catch (Exception localException1) {
			Logger.error("错误：", localException1);
			throw new ServletException(localException1);
		}
	}
}